Java EE and HTML5 Enterprise Application Development by John Brock & Arun Gupta & Geertjan Wielenga

Java EE and HTML5 Enterprise Application Development by John Brock & Arun Gupta & Geertjan Wielenga

Author:John Brock & Arun Gupta & Geertjan Wielenga [Brock, John & Gupta, Arun & Wielenga, Geertjan]
Language: eng
Format: epub
Tags: Java, Programming Languages, Computers, -, General, Enterprise Applications, Html
ISBN: 9780071823142
Google: NpCbAgAAQBAJ
Publisher: McGraw Hill Professional
Published: 2014-03-28T00:00:00+00:00


Listing 4-9 JavaScript Methods for WebSocket

Let’s take a closer look at the methods shown in Listing 4-9. Remember that during the initialization, you defined the new WebSocket object as a variable named websocket.

For websocket.onopen(evt) you are simply sending a message to the output variable saying that the connection was successful. This is the first event thrown by the WebSocket connection and is where you would do any additional setup code if you needed it.

For websocket.onclose(evt) you are going to check for the code given by the WebSocket protocol to help determine why the connection was closed. The tricky part with doing this is that the WebSocket specification (RFC 6455) says only that the endpoint “may” send a close code, not that it has to. If a code is sent, it should conform with one of the close codes defined in the IETF specification (http://tools.ietf.org/html/rfc6455#section-7.4). The recommended codes are listed and described (quoting RFC 6455) in Table 4-1. In the code for this game you are only going to check for a couple of specific codes, and just fall back to a generic message that shows the close code for anything else that may be sent by the endpoint. The check for close code 1006 is just in case the application tries to connect to the endpoint and it’s not there. You can’t use the onerror() event for this case because the WebSocket object is not completely created when this happens. It closes before it has been completely opened. The check for close code 1000 is to indicate that the New Game button was clicked and the websocket.close() method was called and processed successfully.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.